package com.example.service.implService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.config.ResponseVO;
import com.example.mapper.ClassAlbumMapper;
import com.example.pojo.ClassAlbum;
import com.example.service.ClassAlbumService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class ClassAlbumServiceImpl implements ClassAlbumService{
    @Resource
    private ClassAlbumMapper classAlbumMapper;
    @Override
    public ResponseEntity<ResponseVO<List<ClassAlbum>>> getClassesAlbumList(Long classId) {
        QueryWrapper<ClassAlbum> classId1 = new QueryWrapper<ClassAlbum>().eq("class_id", classId);
        List<ClassAlbum> classAlbums = classAlbumMapper.selectList(classId1);
        if(!classAlbums.isEmpty()){
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK,"获取成功",classAlbums));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST,"获取失败"));
    }

    @Override
    public ResponseEntity<ResponseVO<IPage<ClassAlbum>>> getClassesAlbumPage(Integer currentPage, Integer limit,Long classId) {
        Page<ClassAlbum> page = new Page<>(currentPage,limit);
        Page<ClassAlbum> classAlbumPage = classAlbumMapper.selectPage(page, new QueryWrapper<ClassAlbum>().eq("class_id", classId).eq("class_id", classId));
        if(classAlbumPage.getSize()>0){
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK,"获取成功",classAlbumPage));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST,"获取失败"));
    }

    @Override
    public ResponseEntity<ResponseVO<String>> addOrUpdateClassAlbum(ClassAlbum classAlbum) {
        int insert = 0;
        if(Objects.isNull(classAlbum.getClassAlbumId())){
            insert = classAlbumMapper.insert(classAlbum);
        }else {
            insert = classAlbumMapper.updateById(classAlbum);
        }
        if(insert>0){
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK,"操作成功"));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST,"操作失败"));
    }

}
